<?php
/*
	服务公司：沧州讯呗网络科技有限公司
	作者：sun
*/
namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\CartRepository;
use App\Repositories\DeliveryRepository;
use App\Repositories\ProductRepository;
use App\Repositories\AddressRepository;
use App\Traits\BaseFun;
use App\Repositories\XuniRepository;

class CartController extends Controller
{
    public function __construct(
	CartRepository $CartRepository,
	DeliveryRepository $DeliveryRepository,
	ProductRepository $ProductRepository,
	AddressRepository $AddressRepository,
	XuniRepository  $XuniRepository
	)
    {
        $this->CartRepository=$CartRepository;
        $this->DeliveryRepository=$DeliveryRepository;
        $this->ProductRepository=$ProductRepository;
        $this->AddressRepository=$AddressRepository;
		$this->XuniRepository = $XuniRepository;
    }
    public function Cart(Request $request)
    {   
        $res['cart']=$this->CartRepository->list2(['userid'=>$request->userId]);
		
		foreach($res['cart'] as $k => $val) {
			$res['cart'][$k]['cart_pro'] = $this->ProductRepository->info(['id'=>$val['proid']]);
			if($res['cart'][$k]['cart_pro'] == []) {
				$res['cart'][$k]['cart_pro'] = $this->XuniRepository->info(['id' => $val['xuid']]);
			}
		}
		
		
        $res['address']=$this->AddressRepository->info(['userid'=>$request->userId,'state'=>1]);
        $res['count']=$this->CartRepository->cartIn($request->userId);//购物车商品数量
        $countPrice=$this->CartRepository->countPrice($request->userId);//购物车商品数量
        $deliveryInfo=$this->DeliveryRepository->info(['id'=>1]);
        if ($deliveryInfo['type']==1) {
            if ($deliveryInfo['peisong']>$countPrice['cPrice']) {//配送金额小于购买金额 增加配送费
                $res['psprice']=$deliveryInfo['psprice'];
            }else{
                $res['psprice']=0;
            }
        }else{
            // $cartSum=$this->CartRepository->countIn($cid);//获取购物车商品总数
            if ($deliveryInfo['peisong']>$res['count']) {//商品数量小于配送数量 增加配送费
                $res['psprice']=$deliveryInfo['psprice'];
            }else{
                $res['psprice']=0;
            }
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function AddCart(Request $request)//添加购物车
    {
        $cartInfo=$this->CartRepository->info(['proid'=>$request->proid,'userid'=>$request->userId]);
        if ($cartInfo) {
            $carNum=$cartInfo['num']+1;
            $res=$this->CartRepository->update(['id'=>$cartInfo['id'],'num'=>$carNum]);//修改购物车
        }else{
            $Pro=$this->ProductRepository->info(['id'=>$request->proid]);
            $res=$this->CartRepository->create(['userid'=>$request->userId,'proid'=>$Pro['id'],'addrid'=>$request->addrid,'price'=>$Pro['price'],'num'=>1]);//添加购物车
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '购物车操作失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ReduceCart(Request $request)//减购物车
    {
  
        $cartInfo=$this->CartRepository->info(['proid'=>$request->proid,'userid'=>$request->userId]);
        $carNum=$cartInfo['num']-1;
        if ($carNum) {
            $res=$this->CartRepository->update(['id'=>$cartInfo['id'],'num'=>$carNum]);//修改购物车
        }else{
            $res=$this->CartRepository->delete($cartInfo['id']);//删除购物车
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '购物车操作失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CartDel(Request $request)
    {
        $ids=explode(',', $request->cartid);
        $res=$this->CartRepository->allDel($ids);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败或没有此数据!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
}
